~ chicken-core (master) /manual/Module (chicken fixnum)


 1[[tags: manual]]
 2[[toc:]]
 3
 4== Module (chicken fixnum)
 5
 6Because CHICKEN supports a full numeric tower, operations can
 7sometimes incur a substantial overhead to simply detect the type of numbers
 8you're passing in.  When you know you're definitely dealing only with
 9fixnums, you can choose to use fixnum-specific operations to avoid
10this overhead.
11
12This is purely a performance hack.  You might want to consider adding
13[[Types|type annotations]] instead, this often gives the same
14performance boost without having to rewrite all numeric operators in
15your code.
16
17
18=== Arithmetic fixnum operations
19
20<procedure>(fx+ N1 N2)</procedure>
21<procedure>(fx- N1 N2)</procedure>
22<procedure>(fx* N1 N2)</procedure>
23<procedure>(fx/ N1 N2)</procedure>
24<procedure>(fxmod N1 N2)</procedure>
25<procedure>(fxrem N1 N2)</procedure>
26<procedure>(fxneg N)</procedure>
27<procedure>(fxlen N)</procedure>
28<procedure>(fxmin N1 N2)</procedure>
29<procedure>(fxmax N1 N2)</procedure>
30<procedure>(fxand N1 N2)</procedure>
31<procedure>(fxior N1 N2)</procedure>
32<procedure>(fxxor N1 N2)</procedure>
33<procedure>(fxnot N)</procedure>
34<procedure>(fxshl N1 N2)</procedure>
35<procedure>(fxshr N1 N2)</procedure>
36<procedure>(fxgcd N1 N2)</procedure>
37
38{{fx+}} and friends are arithmetic fixnum operations.  These procedures do not
39check their arguments, so non-fixnum parameters will result in incorrect
40results. {{fxneg}} negates its argument, {{fxlen}} returns the integer length
41in bits.
42
43On division by zero, {{fx/}}, {{fxmod}} and {{fxrem}} signal a
44condition of kind {{(exn arithmetic)}}.
45
46{{fxshl}} and {{fxshr}} perform arithmetic shift left and right,
47respectively.
48
49=== Overflow-aware fixnum operations
50
51<procedure>(fx+? N1 N2)</procedure>
52<procedure>(fx-? N1 N2)</procedure>
53<procedure>(fx*? N1 N2)</procedure>
54<procedure>(fx/? N1 N2)</procedure>
55
56These procedures behave similarly to their standard counterparts with
57the exception that {{#f}} is returned if an argument is not a fixnum
58or the result of the operation overflows.
59
60Chaining of such procedures is well-defined and causes the overflow
61error to be propagated.
62
63=== Fixnum comparison and predicates
64
65<procedure>(fxodd? N)</procedure>
66<procedure>(fxeven? N)</procedure>
67<procedure>(fx= N1 N2)</procedure>
68<procedure>(fx> N1 N2)</procedure>
69<procedure>(fx< N1 N2)</procedure>
70<procedure>(fx>= N1 N2)</procedure>
71<procedure>(fx<= N1 N2)</procedure>
72
73Comparison of fixnums and predicates on them.
74
75=== Fixnum limits
76
77<constant>most-positive-fixnum</constant><br>
78<constant>most-negative-fixnum</constant><br>
79<constant>fixnum-bits</constant><br>
80<constant>fixnum-precision</constant><br>
81
82Platform-specific fixnum limits.
83
84---
85Previous: [[Module (chicken file posix)]]
86
87Next: [[Module (chicken flonum)]]
Trap